package ophiux.wechatpay.sdk.common.parse.map;

import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import ophiux.wechatpay.sdk.OphiuxWeChatPaySDKException;
import ophiux.wechatpay.sdk.common.parse.EntityConverter;
import ophiux.wechatpay.sdk.common.parse.IConvert;
import ophiux.wechatpay.sdk.common.writer.MapWriter;

public class MapConverter implements IConvert {

	@Override
	public <T> T toEntity(Class<T> clzz, String dataSource)
			throws OphiuxWeChatPaySDKException {
		return null;
	}

	@Override
	public <T> String entityToString(T obj) throws OphiuxWeChatPaySDKException {
		return null;
	}

	@Override
	public <T> SortedMap entityToMap(T obj) throws OphiuxWeChatPaySDKException {
		
		return parseEntityToMap(obj);
	}
	
	public static <T> SortedMap parseEntityToMap(T obj) throws OphiuxWeChatPaySDKException{
		MapWriter writer = new MapWriter() {
			@Override
			public <T> SortedMap writerFromArray(List<T> valueList)
					throws OphiuxWeChatPaySDKException {
				SortedMap<String, Object> listMap = new TreeMap<String, Object>();
				if(valueList != null && valueList.size() != 0){
					for(T obj : valueList){
						String objStr = "";
						SortedMap<String, Object> objMap = parseEntityToMap(obj);
						listMap.putAll(objMap);
					}
				}
				return listMap;
			}

			@Override
			public SortedMap writerFromObject(Object obj)
					throws OphiuxWeChatPaySDKException {
				return parseEntityToMap(obj);
			}
			
		};
		return EntityConverter.writer2Map(obj, writer);
	}
	
}
